package scalab.utils

object Path{
	import java.io.File

	/*def apply(s: String): Pathname = new Pathname{
		override val string: String = s	
		override def equals(that: Any): Boolean = 
				that.isInstanceOf[Pathname] && 
				Pathname.compareDesc(that.asInstanceOf[Pathname].string, this.string) == 0
	}*/

	/**
	* Tranforms the <code>path</code> given in argument into a new system-dependent
	*	pathname. This method simply replaces '/' characters with its platform-specific 
	*	equivalent (i.e. '/' on unix platforms, '\' on windows).
	*	As an example, you may write things like this to make your build process 
	*	cross-platform:<br><center>
	*		<code>val libPath = Path("projects/projectName/bin/lib/")</code>
	*	</center>
	*/
	def apply(path: String): String = path.replace('/', File.separatorChar)

	/*def reverse(s: String): String = {
		val length: Int = s.length
		val sb: StringBuffer = new StringBuffer(length)
		var i: Int = length - 1
		while(i >= 0){
			sb append s.charAt(i)
			i -= 1
		}
		sb.toString
	}*/

	def compareDesc(s1: String, s2: String): Int = {
		val l1: Int = s1.length - 1
		val l2: Int = s2.length - 1
		val minL: Int = if(l1 < l2) l1 else l2
		var i: Int = 0
		while(i <= minL){
			val cc: Int = s1.charAt(l1 - i).asInstanceOf[java.lang.Character] compareTo s2.charAt(l2 -i)
			if(cc != 0)return cc
			i += 1
		} 
		//bug if s1.length < s2.length but s1 is a suffix of s2! 
		0
	}

	/*def compareAscDesc(s1: String, s2: String): Int = {
		val l1: Int = s1.length - 1
		val l2: Int = s2.length - 1
		val minL: Int = if(l1 < l2) l1 else l2
		var i: Int = 0
		while(i <= minL){
			val cc: Int = s1.charAt(i).asInstanceOf[java.lang.Character] compareTo s2.charAt(l2 -i)
			if(cc != 0)return cc
			i += 1
		} 
		0
	}*/

	def removePrefix(path: File, prefix: File): String = 
		removePrefix(path, prefix.getCanonicalPath)

	def removePrefix(path: File, prefix: String): String = 
		removePrefix(path.getCanonicalPath, prefix)

	def removePrefix(path: String, prefix: String): String = {
		if(path.startsWith(prefix)){
			val length = prefix.length
			var ret = path substring length
			if(length != 0 && prefix.charAt(0) != File.separatorChar){
				if(!path.isEmpty && path.charAt(0) == File.separatorChar){
					ret = ret substring 1
				}
			}
			ret
		}
		else
			path
	}

	def appendSeparator(path: String): String = if(path endsWith File.separator)
		path
	else
		path + File.separator

	def sortPathnames(canonicalPaths: Iterable[String]) = {
		def path2ordered(p: String): Ordered[String] = new Ordered[String]{
			def compare(that: String): Int = 
				compareDesc(p, that)
		}
		import scala.collection.immutable.TreeSet
		(new TreeSet[String]()(path2ordered _)) ++ canonicalPaths
	}

	def subPath(path: String): String = 
		path substring ((path indexOf File.separator) + 1)

}

trait Pathname /*extends Comparable*/{
	val string: String 	
}

object Pathname{
		def apply(s: String): Pathname = new Pathname{
			override val string: String = s
			override def equals(that: Any): Boolean =
			that.isInstanceOf[Pathname] &&
			Path.compareDesc(that.asInstanceOf[Pathname].string, this.string) == 0
	}
}
